package com.atguigu.gmall.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.cart.entity.CartInfo;
import com.atguigu.gmall.model.cart.vo.AddSuccessVo;
import com.atguigu.gmall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author:lyd
 **/
@RestController
@RequestMapping("/api/inner/cart")
public class CartRpcController {

    @Autowired
    CartService cartService;

    @GetMapping("/add/{skuId}")
    public Result<AddSuccessVo> addCart(@PathVariable("skuId") Long skuId,
                                        @RequestParam("skuNum") Long skuNum) {
        String cartKey = cartService.determineCartKey();
        AddSuccessVo result = cartService.addCart(cartKey, skuId, skuNum);
        return Result.ok(result);
    }

    @GetMapping("/deleteChecked")
    public Result deleteChecked() {
        String cartKey = cartService.determineCartKey();
        cartService.deleteChecked(cartKey);
        return Result.ok();
    }

    /**
     * 获取购物车中选中的商品列表
     */
    @GetMapping("checked/list")
    public Result<List<CartInfo>> checkedList() {
        String cartKey = cartService.determineCartKey();
        List<CartInfo> cartInfos = cartService.checkedList(cartKey);
        return Result.ok(cartInfos);
    }
}
